<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
* Name:  Subjects Controller
*
* Author:  Ervin R. Musngi
*
*/

class Subjects extends MX_Controller
{
	protected $id = 0;
	
	protected $record_per_page;
	
	protected $validations = array(
		array(
			'field'	=> 'code',
			'label' => 'lang:subject_code',
			'rules' => 'trim|htmlspecialchars|required|max_length[10]'
		),
		array(
			'field'	=> 'name',
			'label'	=> 'lang:subject_name',
			'rules'	=> 'trim|htmlspecialchars|required|max_length[30]'
		),
		array(
			'field'	=> 'description',
			'label'	=> 'lang:subject_description',
			'rules' => 'trim|htmlspecialchars|required|max_length[100]'
		)
	
	);
	
	public function __construct()
    {
        parent::__construct();
        $this->load->library('users/auth');
		$this->load->library('form_validation');
		$this->load->library('pagination');
		$this->lang->load('subject');
		
		$this->record_per_page = $this->system_library->settings['record_per_page'];
		
		$this->template
				->set_breadcrumb('Home','dashboard')
		   		->set_partial('header', 'partials/header')
				->set_partial('breadcrumb','partials/breadcrumb')
                ->set_partial('footer', 'partials/footer')
				->set_partial('navigation','partials/admin/menu')
                ->set_layout('default')
				->set_theme('default')
				->append_metadata($this->asset->css('reset.css'))
				->append_metadata($this->asset->css('general.css'))
				->append_metadata($this->asset->css('admin/default.css'))
				->append_metadata($this->asset->css('notifcation.slider.css'))
				->append_metadata($this->asset->css('admin/jquery.accordion.css'))
				->append_metadata($this->asset->js('jquery/jquery-1.6.2.min.js'))
				->append_metadata($this->asset->js('jquery.freeow.js'))
				->append_metadata($this->asset->js('notificationBox.jquery.js'))
				->append_metadata($this->asset->js('jquery_accordion_init.js'));
    }
	
	public function index()
	{
		$this->listing();
	}
	
	public function create()
	{
		$this->form_validation->set_rules($this->validations);
		
		if($this->form_validation->run())
		{
			$subject	= new Subjects_m();
			$subject->code 				= $this->input->post('code');
			$subject->description 		= $this->input->post('description');
			$subject->name				= $this->input->post('name');
			$subject->category_id		= $this->input->post('category');
			$subject->school_level_id	= $this->input->post('year_level');
			$subject->save(); 
			$this->session->set_flashdata('msg_title',lang('subject_success_title'));
			$this->session->set_flashdata('msg',lang('subject_success_insert'));
			redirect('grading/subjects/listing/');
		}
		
		$categories = new Category_m();
		
		$categories->where('is_active',1)->get();
		
		foreach($categories as $c)
		{
			$data['categories'][$c->id] = $c->name;
		}
		
		$year_level = new School_level_m();
		
		$year_level->where('is_active',1)->get();
		
		foreach($year_level as $yl)
		{
			$data['year_level'][$yl->id] = $yl->name;
		}
		
		if (!isAjax())
		{
			$this->template
					->set_breadcrumb('Subjects','grading/subjects/')
					->set_breadcrumb('create')
					->build('subject/create',$data);
		} else {
			$this->load->view('subject/create',$data);
		}
	}
	
	public function listing($page = 0)
	{
		if ($page < 0  || strlen($page) > 13)
		{
			redirect('grading/subjects/listing/');
		}
		else
		{
			$limit = $this->record_per_page;
			
			$subject = new Subjects_m();
			
			$total_rows = $subject->where('is_active',1)->get(($limit + $page),$page)->count();
			
			if($total_rows < $page)
			{
				redirect('grading/subjects/listing/');
			}
			else
			{
				$subject
					->where('is_active',1)
					->include_related('category',array('id','name'),TRUE,TRUE)
					->where_related('category','is_active',1)
					->get($limit,$page);
	
				$data['subjects']		= $subject;
			
				//setup pagination
				$config['base_url']		= site_url('grading/subjects/listing/');
				$config['per_page']		= $limit;
				$config['uri_segment'] 	= 4;
				$config['total_rows']	= $total_rows;
				$config['anchor_class'] = 'class="btn"';
				$config['cur_tag_open'] = ' <div class="btn btn-disabled">';
				$config['cur_tag_close'] = '</div>';
				$this->pagination->initialize($config);
			
				$data['page'] 			= $this->pagination->create_links();
				
				$this->template
							->append_metadata($this->asset->css('facebox.css'))
							->append_metadata($this->asset->js('jquery/facebox.js'))
							->set_breadcrumb('Subjects','')
							->build('subject/list',$data);
			}
		}
	}
	
	public function delete($id='')
	{
		if($this->input->post('subject_id') || $id != '')
		{
			$subject = new Subjects_m();
			
			if($this->input->post('subject_id')){
				$id = implode(',',$this->input->post('subject_id'));
			}
			
			$subject->where('id IN',"({$id})",FALSE)->update('is_active', 0);
			
			$this->session->set_flashdata('msg_title',lang('subject_success_title'));
			$this->session->set_flashdata('msg',lang('subject_success_delete'));
			redirect('grading/subjects/listing');
		}
		else
		{
			
		}
	}
	
	public function edit($id=0)
	{
		$subject = new Subjects_m();
		$sub = $subject->where('id',$id)->get();
		if($subject->result_count() == 0)
		{
			redirect('grading/subjects/listing/');
		}
		else
		{
			$data['subject']	= $sub;

			$this->form_validation->set_rules($this->validations);
			
			if($this->form_validation->run())
			{
				
				$subject->where('id',$id)->update(array(
													'code' 				=> $this->input->post('code'),
													'name' 				=> $this->input->post('name'),
													'description'		=> $this->input->post('description'),
													'category_id'		=> $this->input->post('category'),
													'school_level_id'	=> $this->input->post('year_level')
													));
													
				$this->session->set_flashdata('msg_title',lang('subject_success_title'));
				$this->session->set_flashdata('msg',lang('subject_success_update'));
				
				redirect('grading/subjects/listing/');
			}
			
			$categories = new Category_m();
		
			$categories->where('is_active',1)->get();
			
			foreach($categories as $c)
			{
				$data['categories'][$c->id] = $c->name;
			}
			
			$year_level = new School_level_m();
		
			$year_level->where('is_active',1)->get();
			
			foreach($year_level as $yl)
			{
				$data['year_level'][$yl->id] = $yl->name;
			}
			
			if (isAjax())
			{
				$this->load->view('subject/edit',$data);
			}
			else
			{
				$this->template
							->set_breadcrumb('Subjects','grading/subjects/')
							->set_breadcrumb('Edit')
							->build('subject/edit',$data);
			}
		}
	}
	
}